<template>
  <div>
    <el-card>
      <el-form>
        <el-form-item label="时间范围">
          <el-date-picker
            size="mini"
            v-model="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
          <el-button @click="getOrderTotal" size="mini" type="primary"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="echarts" ref="line"></div>
    </el-card>
  </div>
</template>

<script>
import { api_order_ordertotal } from "@/api/order";
import * as echarts from "echarts";
import { YMDHms } from "@/filters/index";
export default {
  data() {
    return {
      date: [],
    };
  },
  methods: {
    async getOrderTotal() {
      let res = await api_order_ordertotal({
        date: JSON.stringify(this.date),
      });
      let { data } = res.data;
      // 处理数据
      let xData = data.map((v) => YMDHms(v.orderTime));
      let series = [
        {
          type: "bar",
          name: "金额数据",
          data: data.map((v) => v.orderAmount),
        },
      ];
      //   画图
      this.renderLine({
        xData,
        series,
        title: "订单统计",
      });
    },
    // 画折线图
    renderLine(obj) {
      //   默认值
      let defaultOptions = {
        title: "默认标题",
      };
      //   最终参数
      let finalObj = { defaultOptions, ...obj };
      let myChart = echarts.init(this.$refs.line);
      //  配置项
      let options = {
        title: {
          text: finalObj.title,
          left: 15,
          top: 15,
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          top: 15,
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: finalObj.xData,
        },
        yAxis: {
          type: "value",
        },
        // 核心数据
        series: finalObj.series,
      };
      //   绘制图表
      myChart.setOption(options);
    },
  },

  mounted() {
    this.getOrderTotal();
  },
};
</script>

<style lang="scss" scoped>
.el-button {
  margin-left: 15px;
}
.echarts {
  height: 400px;
  margin-top: 50px;
}
</style>